<template>
  <el-dialog title="人员添加" :visible.sync="dialogVisible" width="30%" :before-close="handleClose">
    <el-form ref="form" :model="form" label-width="100px">
      <el-form-item label="用户名">
        <el-input v-model="form.username"></el-input>
      </el-form-item>
      <el-form-item label="密码">
        <el-input v-model="form.password"></el-input>
      </el-form-item>
      <el-form-item label="手机号">
        <el-input v-model="form.mobile"></el-input>
      </el-form-item>
      <el-form-item label="性别">
        <el-select v-model="form.sex" placeholder="请选择">
        <el-option   label="男" value="男"></el-option>
        <el-option   label="女" value="女"></el-option>
      </el-select>
      </el-form-item>
      <el-form-item label="职位">
        <el-select v-model="form.roleId" placeholder="请选择职位">
          <el-option v-for="v in  sysrole" :key="v.id"  :label="v.roleName" :value="v.id"></el-option>
        </el-select>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="dialogVisible = false">取 消</el-button>
      <el-button type="primary" @click="create">确 定</el-button>
    </span>
  </el-dialog>
</template>

<script>
import api from '../../../api'
import qs from "qs";
export default {
  data() {
    return {
      dialogVisible: false,
      form: {
        username: '', 
        roleId:'',
        password:'',
        mobile:'',
        sex:''
      },
      sysrole:[]
    }
  },
  mounted() {
    this.$bus.$on('onAddPersonShow', (flag) => {
      this.dialogVisible = flag
    })
    this.findBySysRole()
  },
  methods: {
    findBySysRole(){
      api.sysRolefindByAll().then(res=>{
        this.sysrole = res.data.data
      }).catch(err=>{
        console.log(err)
      })
    },
    handleClose(done) {
      this.$confirm('确认关闭？')
        .then((_) => {
          done()
        })
        .catch((_) => {})
    },
    create() {
      
      api.personInfosave(qs.stringify(this.form)).then(res=>{
        if(res.data.code == 2001){
                this.$message({
                  type: 'success',
                  message: '添加成功!'
                });
           this.dialogVisible = false
            this.$bus.$emit("onRefreshPerson",true)
       }
      }).catch(err=>{

      })
      
    },
    onSubmit() {
      console.log('submit!')
    }
  }
}
</script>

<style>
</style>